import { getNews24List, getNews24Count, getNews24History } from '../../../services/news_service';

export default {
  namespace: 'newstracking',
  state: {
    newsData: {
      code: 0,
      message: '',
      data: [],
    },
  },

  effects: {
    *GetNews24List({ payload, callback }, { call, put }) {
      const response = yield call(getNews24List, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveNewsData',
        payload: response,
      });
      if (callback) callback(response);
    },
    *GetNews24ListAdd({ payload, callback }, { call, put }) {
      const response = yield call(getNews24History, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveNewsDataAdd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *GetNews24ListReloadCount({ payload, callback }, { call, put }) {
      const response = yield call(getNews24Count, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
    },
    *GetNews24ListReload({ payload, callback }, { call, put }) {
      const response = yield call(getNews24List, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveNewsDataReload',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    saveNewsData(state, action) {
      return {
        ...state,
        newsData: action.payload,
      };
    },
    saveNewsDataAdd(state, action) {
      return {
        ...state,
        newsData: {
          code: action.payload.code,
          message: action.payload.message,
          data: state.newsData.data.concat(action.payload.data),
        },
      };
    },
    saveNewsDataReload(state, action) {
      return {
        ...state,
        newsData: {
          code: action.payload.code,
          message: action.payload.message,
          data: [].concat(action.payload.data).concat(state.newsData.data),
        },
      };
    },
  },
};
